/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author clement
 */
public class get_history extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            Database bd = new Database();
            bd.connect();
            String viewHistoryID = "";
            if (request.getParameter("view") != null) {
                viewHistoryID = request.getParameter("view");
            }

            if (!viewHistoryID.equals("")) {
  
                ResultSet res = bd.exec("Select * FROM RMIHISTO Where id_histo='" + viewHistoryID + "';");
                if (res != null) {
                    try {
                        res.next();

                        out.println("<h1>History Number " + res.getString("id_histo") + "</h1>");
                        out.println("<h2>(On " + res.getString("ip_serveur") + ")</h2>");
                        out.println("<br /><br />");
                        String method_name = "";
                        method_name = res.getString("method_name");


                        if (method_name.equalsIgnoreCase("Memory")){
                            out.println("<h4>Memory</h4>"
                            + "<p>"
                            + "Free : " + res.getString("method_value")
                            + "</p>");
                            
                        }else if(method_name.equalsIgnoreCase("CPU")){
                            out.println("<h4>CPU</h4>"
                            + "<p>"
                            + "User Usage : " + res.getString("method_value")
                            + "</p>");

                        } else if (method_name.equalsIgnoreCase("Processus")) {
                            out.println("<h4>Processus</h4>"
                            + "<table id=\"processtable\" class=\"table-hover\">"
                            + "<thead>"
                            + "<tr>"
                            + "<th>PID</th>"
                            + "<th>Command</th>"
                            + "<th>User</th>"
                            + "<th>%CPU</th>"
                            + "<th>%Memory</th>"
                            + "<th>Priority</th>"
                            + "</tr>"
                            + "</thead>"
                            + "<tbody>");
                            
                            List<Proc> list_proc = null;
                            try {
                                list_proc = bd.getObject(res.getString("id_histo"));
                            } catch (Exception ex) {
                                Logger.getLogger(get_history.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            
                            //Boucle DB Object or DB Tables
                            for (Proc process : list_proc) {
                            out.println("<tr>"
                                + "<td>" + process.getPid() + "</td>"
                                + "<td>" + process.getCommand() + "</td>"
                                + "<td>" + process.getUser() + "</td>"
                                + "<td>" + process.getCpu() + "</td>"
                                + "<td>" + process.getMem() + "</td>"
                                + "<td>" + process.getPriorite() + "</td>"
                                + "</tr>");
                            }
                            //END Boucle
                            
                            out.println("</tbody>"
                            + "</table>"
                            + "<link href=\"plugins/chromatable/css/style.css\" rel=\"stylesheet\">"
                            + "<script src=\"plugins/chromatable/jquery.chromatable.js\"></script>"
                            + "<script type=\"text/javascript\">"
                            + "$(\"#processtable\").chromatable({"
                            + "scrolling: \"yes\","
                            + "width: '600px'"
                            + "});"
                            + "</script>");
                            
                        }else{
                            out.println("<div class=\"alert alert-error\">"
                        + "<p>unknown method : "+method_name+"</p>"
                        + "</div>");
                        }


                    } catch (SQLException ex) {
                        Logger.getLogger(get_history.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }else{
                    out.println("<div class=\"alert alert-error\">"
                        + "<p>This history id doesn't exist.</p>"
                        + "</div>");
                }
            }

            bd.close();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
